package com.jp.knowledge.adapter.attention;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.model.AttentionData;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;
import com.jp.knowledge.util.ImageLoadUtil;
import com.jp.knowledge.util.TimeUtil;

import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/3/17.
 */

public class AttentionListAdapter extends RecycleVAdapter<AttentionData> implements View.OnClickListener {

    private boolean showTopBackground;

    public AttentionListAdapter(Context context, @NonNull List<AttentionData> list) {
        this(context, list, false);
    }

    public AttentionListAdapter(Context context, @NonNull List<AttentionData> list, boolean showTopBackground){
        super(context, list);
        this.showTopBackground = showTopBackground;
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.attention_item_view;
    }

    @Override
    public void onBind(RecycleVHolder holder, int position) {
        AttentionData attentionData = getData(position);

        ImageView icon = holder.get(R.id.icon);
        TextView title = holder.get(R.id.title);
        TextView description = holder.get(R.id.descritption);
        TextView newNum = holder.get(R.id.new_num);
        TextView newText = holder.get(R.id.new_text);
        TextView companyName = holder.get(R.id.company_name);
        TextView city = holder.get(R.id.city);
        TextView count = holder.get(R.id.count);
        TextView time = holder.get(R.id.time);
        TextView attentionNum = holder.get(R.id.attetion_num);
        TextView isCreate = holder.get(R.id.is_create);

        if(attentionData.getIcon() != null && attentionData.getIcon().length() > 0)
//            ImageLoadUtil.load(context, attentionData.getIcon() + "?imageView2/2/w/100/interlace/1", icon);
            ImageLoadUtil.loadrund(mContext,
                    attentionData.getIcon() + "?imageView2/2/w/100/interlace/1",
                    icon, 20);
        else if(attentionData.getType() == 3)
            icon.setImageResource(R.mipmap.intelligent_analytics);
        else if(attentionData.getType() == 8)
            icon.setImageResource(R.mipmap.intelligent_analysis_products);
        else if(attentionData.getType() == 4)
            icon.setImageResource(R.mipmap.intelligent_analysis_character);
        else if(attentionData.getType() == 18)
            icon.setImageResource(R.mipmap.keyword);
        else
            icon.setVisibility(View.GONE);
        setTextView(title, attentionData.getTitle());
//        setTextView(description, attentionData.getDesc());
        setTextView(description, attentionData.getSource());
        newNum.setText(attentionData.getNewNum()+"");
        if(attentionData.getNewNum() > 0)
            newNum.setTextColor(mContext.getResources().getColor(R.color.orange));
        else
            newNum.setTextColor(mContext.getResources().getColor(R.color.font_gray_think));
        newText.setText(attentionData.getCountNum()+"数据");
        setTextView(companyName, attentionData.getSource());
        setTextView(city, attentionData.getAddress());
        setTextView(count, attentionData.getCountNum()+"数据");
        setTextView(time, TimeUtil.friendly_time(attentionData.getDataLatestTime()));
        setTextView(attentionNum, attentionData.getSubscribeNum() + "关注");

        // 除时间外隐藏其他控件
        companyName.setVisibility(View.GONE);
        city.setVisibility(View.GONE);
        count.setVisibility(View.GONE);
        attentionNum.setVisibility(View.GONE);
        isCreate.setVisibility(View.GONE);

        holder.get(R.id.main_view).setTag(position);
        holder.get(R.id.delete_btn).setTag(position);
        holder.get(R.id.main_view).setOnClickListener(this);
        holder.get(R.id.delete_btn).setOnClickListener(this);
//        holder.get(R.id.change_btn).setVisibility(View.GONE);
        holder.get(R.id.main_view).setBackground(mContext.getResources().getDrawable(R.drawable.normal_click_bg));
        if(attentionData.getIsTop() == 0) {
            holder.setTextView(R.id.change_btn, "置顶");
            if(showTopBackground)
                holder.getConvertView().setBackgroundColor(mContext.getResources().getColor(R.color.white));
        } else {
            holder.setTextView(R.id.change_btn, "取消置顶");
            if(showTopBackground)
                holder.getConvertView().setBackgroundColor(mContext.getResources().getColor(R.color.attention_top_bg_color));
        }
        holder.get(R.id.change_btn).setTag(position);
        holder.get(R.id.change_btn).setOnClickListener(this);

//        holder.setTextView(R.id.delete_btn, "取消");
    }

    private void setTextView(TextView textView, String str){
        if(str == null || str.trim().length() == 0) {
            textView.setVisibility(View.GONE);
        } else{
            textView.setText(str);
            textView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.main_view:
                if(this.callback != null)
                    callback.onItemClick((int)v.getTag());
                break;
            case R.id.delete_btn:
                if(this.callback != null)
                    callback.onDeleteClick((int)v.getTag());
                break;
            case R.id.change_btn:
                if(this.callback != null)
                    callback.onStickTopClick((int)v.getTag(), getData((int)v.getTag()).getIsTop()==0);
                break;
        }
    }

    private OnItemCallback callback;
    public void setOnItemCallback(OnItemCallback callback){
        this.callback = callback;
    }
    public interface OnItemCallback{
        void onItemClick(int position);
        void onStickTopClick(int position, boolean isTop);
        void onDeleteClick(int position);
    }
}
